package com.wchat.controller;

import com.wchat.service.MediaTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author : willian fu
 * @date : 2021/8/31
 */
@RestController
@RequestMapping("media")
public class MediaTransferController {

    @Autowired
    private MediaTransferService mediaTransferService;

    /**
     * 上传头像 无需登录也可以
     * @param file 文件
     * @return {
     *     "original": "20200621203.jpg", 原始文件名
     *     "source": "0716299e39dd30a800e349dff5385465.jpg" //新文件名
     * }
     */
    @PostMapping("upload/avatar")
    public Object uploadAvatar(@RequestParam("file") MultipartFile file){
        return mediaTransferService.uploadAvatar(file);
    }

    /**
     * 上传图片
     * @param file 图片
     * @return 图片在服务器上的名称
     */
    @PostMapping("upload/image")
    public Object uploadImage(@RequestParam("file")MultipartFile file){
        return mediaTransferService.uploadImage(file);
    }

    /**
     * 上传文件
     * @param file 文件
     * @return 文件在服务器上的名称
     */
    @PostMapping("upload/file")
    public Object uploadFile(@RequestParam("file")MultipartFile file){
        return mediaTransferService.uploadFile(file);
    }

    /**
     * 获取头像
     * @param rsp
     * @param fileName
     */
    @GetMapping("get/avatar")
    public void getAvatar(HttpServletResponse rsp, @RequestParam String fileName){
        mediaTransferService.getImage(rsp, false, fileName);
    }

    @GetMapping("get/image")
    public void getImage(HttpServletResponse rsp, @RequestParam String fileName){
        mediaTransferService.getImage(rsp, true, fileName);
    }

    @GetMapping("get/file")
    public void getResource(HttpServletResponse rsp, @RequestParam String fileName){
        mediaTransferService.getResource(rsp, fileName);
    }
}
